package com.example;

import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;
import org.apache.commons.net.ftp.FTPClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: fransvanderlek
 * Date: 6/16/12
 * Time: 10:37 AM
 * To change this template use File | Settings | File Templates.
 */
public class UploadBackgroundTask extends SynchronizationTask {

    public UploadBackgroundTask(MainViewHolder view, FTPClient ftpClient) {
        super(view, ftpClient);
    }


    @Override
    protected List<File> doInBackground(List<File>... files) {

        List<File> toDo = new ArrayList<File>();
        toDo.addAll(files[0]);
        List<File> pending = files[0];


        for (File file : toDo) {
            FileInputStream fis = null;
            OutputStream out = null;

            try {
                fis = new FileInputStream(file);
                ftpClient.storeFile(file.getName(), fis);
                pending.remove(file);

                //this.remainingFiles = toDo.toArray(new File[]{});

                Log.d("UPLOAD","Uploaded "+file.getName()+" todo: "+ toDo.toString());

                publishProgress(pending);

            } catch (IOException e) {

            } finally {
                try {
                    if ( fis !=null)
                    {
                        fis.close();
                    }

                    if (out !=null ) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.

                }
            }

        }


        return pending;
    }


}
